﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Ext.Net;
using Ext.Net.Utilities;
using Newtonsoft.Json.Serialization;
using Sharp.Infrastructure.Search;

namespace Sharp.Portal.ExtNet
{
    /// <summary>
    /// Ext控件的请求参数的实体类
    /// </summary>
    /// <remarks>整体复制Ext.Net.ExtRequestParameters，为了增加Optimizes属性</remarks>
    [DataContract]
    public class ExtRequestParameters
    {
        private Dictionary<string, object> extraParams;

        /// <summary>
        ///
        /// </summary>
        public int Page
        {
            get
            {
                if (this.ContainsKeyAndNotNull("page"))
                {
                    return Convert.ToInt32(this.extraParams["page"]);
                }
                return -1;
            }
        }

        /// <summary>
        ///
        /// </summary>
        public int Start
        {
            get
            {
                if (this.ContainsKeyAndNotNull("start"))
                {
                    return Convert.ToInt32(this.extraParams["start"]);
                }
                return -1;
            }
        }

        /// <summary>
        ///
        /// </summary>
        public int Limit
        {
            get
            {
                if (this.ContainsKeyAndNotNull("limit"))
                {
                    return Convert.ToInt32(this.extraParams["limit"]);
                }
                return -1;
            }
        }

        /// <summary>
        ///
        /// </summary>
        public DataSorter[] Sort
        {
            get
            {
                string text = this.ContainsKeyAndNotNull("sort") ? this.extraParams["sort"].ToString() : "";
                if (StringUtils.IsNotEmpty(text) && text.StartsWith("[") && text.EndsWith("]"))
                {
                    return JSON.Deserialize<DataSorter[]>(this.extraParams["sort"].ToString(), new CamelCasePropertyNamesContractResolver());
                }
                return new DataSorter[0];
            }
        }
        /// <summary>
        /// 排序
        /// </summary>
        public string SimpleSort
        {
            get
            {
                DataSorter[] sort = this.Sort;
                if (sort.Length != 0)
                {
                    return sort[0].Property;
                }
                if (!this.ContainsKeyAndNotNull("sort"))
                {
                    return null;
                }
                return this.extraParams["sort"].ToString();
            }
        }
        /// <summary>
        /// 排序方式
        /// </summary>
        public SortDirection SimpleSortDirection
        {
            get
            {
                DataSorter[] sort = this.Sort;
                if (sort.Length != 0)
                {
                    return sort[0].Direction;
                }
                string text = this.ContainsKeyAndNotNull("dir") ? this.extraParams["dir"].ToString() : null;
                if (!StringUtils.IsNotEmpty(text))
                {
                    return SortDirection.Default;
                }
                return (SortDirection)Enum.Parse(typeof(SortDirection), text, true);
            }
        }

        /// <summary>
        ///
        /// </summary>
        public DataFilter[] Filter
        {
            get
            {
                if (this.ContainsKeyAndNotNull("filter"))
                {
                    return JSON.Deserialize<DataFilter[]>(this.extraParams["filter"].ToString(), new CamelCasePropertyNamesContractResolver());
                }
                return new DataFilter[0];
            }
        }

        /// <summary>
        ///
        /// </summary>
        public FilterConditions GridFilters
        {
            get
            {
                if (this.ContainsKeyAndNotNull("filter"))
                {
                    return new FilterConditions(this.extraParams["filter"].ToString());
                }
                return null;
            }
        }

        /// <summary>
        ///
        /// </summary>
        public DataSorter[] Group
        {
            get
            {
                string text = this.ContainsKeyAndNotNull("group") ? this.extraParams["group"].ToString() : "";
                if (StringUtils.IsNotEmpty(text) && text.StartsWith("[") && text.EndsWith("]"))
                {
                    return JSON.Deserialize<DataSorter[]>(this.extraParams["group"].ToString(), new CamelCasePropertyNamesContractResolver());
                }
                return new DataSorter[0];
            }
        }
        /// <summary>
        /// 分组
        /// </summary>
        public string SimpleGroup
        {
            get
            {
                DataSorter[] group = this.Group;
                if (group.Length != 0)
                {
                    return group[0].Property;
                }
                if (!this.ContainsKeyAndNotNull("group"))
                {
                    return null;
                }
                return this.extraParams["group"].ToString();
            }
        }
        /// <summary>
        /// 分组方式
        /// </summary>
        public SortDirection SimpleGroupDirection
        {
            get
            {
                DataSorter[] group = this.Group;
                if (group.Length != 0)
                {
                    return group[0].Direction;
                }
                string text = this.ContainsKeyAndNotNull("groupDir") ? this.extraParams["groupDir"].ToString() : null;
                if (!StringUtils.IsNotEmpty(text))
                {
                    return SortDirection.Default;
                }
                return (SortDirection)Enum.Parse(typeof(SortDirection), text, true);
            }
        }

        /// <summary>
        /// The query parameter which might be sent if a Store is associated with a ComboBox
        /// </summary>
        public string Query
        {
            get
            {
                if (this.ContainsKeyAndNotNull("query"))
                {
                    return this.extraParams["query"].ToString();
                }
                return null;
            }
        }
        /// <summary>
        /// Ext控件的请求参数的实体类
        /// </summary>
        public ExtRequestParameters() : this(HttpContext.Current)
        {
        }
        /// <summary>
        /// Ext控件的请求参数的实体类
        /// </summary>
        /// <param name="extraParams"></param>
        public ExtRequestParameters(Dictionary<string, object> extraParams)
        {
            this.extraParams = extraParams;
        }
        /// <summary>
        /// Ext控件的请求参数的实体类
        /// </summary>
        /// <param name="extraParams"></param>
        public ExtRequestParameters(string extraParams)
        {
            this.extraParams = JSON.Deserialize<Dictionary<string, object>>(extraParams);
        }
        /// <summary>
        /// Ext控件的请求参数的实体类
        /// </summary>
        /// <param name="context"></param>
        public ExtRequestParameters(HttpContext context)
        {
            this.extraParams = new Dictionary<string, object>();
            this.extraParams["page"] = context.Request["page"];
            this.extraParams["start"] = context.Request["start"];
            this.extraParams["limit"] = context.Request["limit"];
            this.extraParams["sort"] = context.Request["sort"];
            this.extraParams["filter"] = context.Request["filter"];
            this.extraParams["group"] = context.Request["group"];
            this.extraParams["query"] = context.Request["query"];
        }

        private bool ContainsKeyAndNotNull(string key)
        {
            return this.extraParams.ContainsKey(key) && this.extraParams[key] != null;
        }

        /// <summary>
        /// 查询优化
        /// </summary>
        /// <remarks>比如主数据的查询用like就浪费性能</remarks>
        public List<ComparisonOptimize> Optimizes { get; set; }
    }
}
